package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private MongoTemplate template;

    @Autowired
    private StrategyCommentRepository repository;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        repository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        repository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        StrategyComment strategyComment =repository.findById(id).orElse(null);
        return strategyComment;
    }

    @Override
    public List<StrategyComment> list() {
        return repository.findAll();
    }

  @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        //分页查询
        //查询对象
        //1: 查询总数:total
        Query query = new Query();
        long total = template.count(query, StrategyComment.class);
        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        if(total == 0){
            return Page.empty();  //空的分页对象
        }
        //2: 每页显示条数集合: list
        //设置页面显示条数, 还有当前页
        //1 当前页 2 每页的条数 3 排序规则 4 排序字段
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC, "_id");
        query.with(pageable);

        List<StrategyComment> list = template.find(query, StrategyComment.class);
        //3:封装page对象
        return new PageImpl<StrategyComment>(list,pageable,total);
    }

    @Override
    public void commentThumb(String cid, String id) {
        //查询评论对象也就是当前对象id
        StrategyComment strategyComment = this.get(cid);
        //获取点赞用户集合
        List<String> list = strategyComment.getThumbuplist();
        if (list.contains(id)){
            //如果存在 就表示取消点赞 点赞数减一 并且移除点赞id
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            list.remove(id);
        }else {
            //如果不存在 就表示点赞操作 点赞数加1 并且往集合中添加点赞用户id
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            list.add(id);
        }
        //更新操作
        this.update(strategyComment);



    }
    }

    /*@Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        //目的地id 在攻略分类中间接获取
        StrategyCommentCatalog strategyCommentCatalog = strategyCommentCatalogService.get(strategyComment.getCatalogId());
        strategyComment.setDestId(strategyCommentCatalog.getDestId());
        // 目的地name
        strategyComment.setDestName(strategyCommentCatalog.getDestName());
        //主题名称 从攻略主题
        StrategyCommentTheme theme = strategyCommentThemeService.get(strategyComment.getThemeId());
        strategyComment.setThemeName(theme.getName());
        //分类名称
        strategyComment.setCatalogName(strategyCommentCatalog.getName());
        //是否国外
        List<Destination> toasts = destinationService.findtoasts(strategyCommentCatalog.getDestId());
        if(toasts.size()>0 && toasts !=null){
            Destination destination = toasts.get(0);
            if ("中国".equals(destination.getName())){
                strategyComment.setIsabroad(StrategyComment.ABROAD_NO);
            }else {
                strategyComment.setIsabroad(StrategyComment.ABROAD_YES);
            }
        }
        if (StringUtils.hasLength(strategyComment.getId())){
            //编辑
            if (StringUtils.hasLength(strategyComment.getId())){
                //查询
                StrategyComment sty = this.get(strategyComment.getId());
                //替换
                strategyComment.setViewnum(sty.getViewnum());
                strategyComment.setReplynum(sty.getReplynum());
                strategyComment.setFavornum(sty.getFavornum());
                strategyComment.setSharenum(sty.getSharenum());
                strategyComment.setThumbsupnum(sty.getThumbsupnum());
                strategyComment.setCreateTime(sty.getCreateTime());
            }
            this.update(strategyComment);
        }else {
            //添加
            //创建时间
            strategyComment.setCreateTime(new Date());
            this.save(strategyComment);

        }
    }*/

    /*@Override
    public void chenagState(int state, String id) {
        //部分字段更新
        //创建查询对象
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));//where
        Update update = new Update();//$set
        update.set("state",state);//state=1
        template.updateMulti(query,update,StrategyComment.class);
    }

    @Override
    public List<StrategyComment> findByCatalogId(String id) {

        return   repository.findByCatalogId(id);
    }

    @Override
    public List<StrategyComment> queryviewnumTop3(String destId) {
        //目的地id指定了 点击量倒数 截热门三个
        PageRequest pageRequest = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<StrategyComment> list = repository.findByDestId(destId, pageRequest);
        return   list;
    }*/

